package com.github.cosycode.imgtool.action;

import com.github.cosycode.common.ext.bean.DoubleBean;
import com.github.cosycode.common.lang.BaseRuntimeException;
import com.github.cosycode.flow.base.AbstractAction;
import com.github.cosycode.imgtool.ctrl.MarkdownImgLinkMatchAnalyzer;
import com.github.cosycode.imgtool.rule.CheckConfig;
import com.github.cosycode.imgtool.rule.MarkdownRepairSource;
import com.github.cosycode.imgtool.util.LogUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <b>Description : </b>
 * <p>
 * <b>created in </b> 2023/2/4
 * </p>
 *
 * @author CPF
 * @since
 **/
@Slf4j
@AllArgsConstructor
public class MdDanglingLinkMatchAndFixAction extends AbstractAction<MdDanglingLinkMatchAndFixAction.MdLlMfReport> {

    private MarkdownRepairSource source;
    private CheckConfig config;

    /**
     * 1. check img link in markdown file.
     */
    public static MdLlMfReport fixMdPicByMoveImg(MarkdownRepairSource source, CheckConfig config, boolean dryRun) throws IOException {
        MarkdownImgLinkMatchAnalyzer.MarkdownImgLinkMatchReport markdownImgLinkMatchReport =
                MarkdownImgLinkMatchAnalyzer.computeMdImgLinkMatchesInfo(source.getMarkdownFileSet(), source.getPicFileSet(), config);

        MdLlMfReport mdLlMfReport = new MdLlMfReport();
        mdLlMfReport.setDryRun(dryRun);
        mdLlMfReport.setMarkdownImgLinkMatchReport(markdownImgLinkMatchReport);

        // 2. print log?
        markdownImgLinkMatchReport.printLog();
        markdownImgLinkMatchReport.printMatchLog(config.getRootPath());
        markdownImgLinkMatchReport.printNonMatchLog(config.getRootPath());

        Map<String, String> matchedLink = markdownImgLinkMatchReport.getMatchedLink();
        // 移动图片
        if (!matchedLink.isEmpty()) {
            LogUtils.logEvent(() -> {
                matchedLink.forEach((k, v) -> {
                    try {
                        com.github.cosycode.imgtool.util.FileUtils.moveFile(new File(k), new File(v), dryRun);
                    } catch (IOException e) {
                        throw new BaseRuntimeException("", e);
                    }
                });
            }, "移动图片");
            mdLlMfReport.setMessage("移动图片成功");
            mdLlMfReport.setContinue(true);
        }

        List<DoubleBean<String, String>> nonMatchesLinkList = markdownImgLinkMatchReport.getNonMatchesLinkList();
        if (!nonMatchesLinkList.isEmpty()) {
            mdLlMfReport.setMessage("存在未匹配成功的图片，请检查");
            mdLlMfReport.setContinue(false);
        }
        return mdLlMfReport;
    }

    public static void main(String[] args) throws IOException {
        // 1. 数据源
        String projectPath = "P:\\git-note/cpfree/";

        MarkdownRepairSource source = new MarkdownRepairSource();
        source.addMarkdownFile(new File(projectPath));
        source.addImgFile(new File("P:\\git-note/picture-warehouse"));
        source.addImgFile(new File("P:\\git-note/imgbed1"));
        source.addImgFile(new File(projectPath));
        boolean dryRun = true;

        CheckConfig config = new CheckConfig();
//        config.setWebPathEnable(true, false);
        config.setRelatePathEnable(true);
        config.setAbsolutePathEnable(true);
        config.setRootPathEnable(projectPath);
        fixMdPicByMoveImg(source, config, dryRun);
    }

    @Override
    public MdLlMfReport doProcess() throws Exception {
        return fixMdPicByMoveImg(source, config, dryRun);
    }

    @Data
    public static class MdLlMfReport {
        MarkdownImgLinkMatchAnalyzer.MarkdownImgLinkMatchReport markdownImgLinkMatchReport;
        String message;
        boolean isContinue;
        boolean dryRun;
    }

}
